//
//  NSString+ColorAttstr.m
//  CharonTVProject
//
//  Created by karong on 2023/8/25.
//

#import "NSString+ColorAttstr.h"

@implementation NSString (ColorAttstr)
- (NSMutableAttributedString *)stringWithPartHighLightSubstring:(NSString *)substring highLightColor:(UIColor *)color {
    substring = [substring.lowercaseString stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSString * copyTotalString = self.lowercaseString;
    NSMutableString * replaceString = [NSMutableString stringWithString:@" "];
    for (int i = 0; i < substring.length; i ++) {
        NSString *singleString = [substring substringWithRange:NSMakeRange(i, 1)];
        while ([copyTotalString rangeOfString:singleString].location != NSNotFound) {
             NSRange range = [copyTotalString rangeOfString:singleString];
             //颜色如果统一的话可写在这里，如果颜色根据内容在改变，可把颜色作为参数，调用方法的时候传入
             [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
             copyTotalString = [copyTotalString stringByReplacingCharactersInRange:range withString:replaceString];
         }
    }
    return attributedString;
}
@end
